package com.zui.manager;

import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.zui.entity.BaseEntity;
import com.zui.util.Page;

public class EntityManagerImpl<T, ID extends Serializable>  implements EntityManager<T, ID> {

	private EntityDao<T, ID> entityDao;

	public EntityDao<T, ID> getEntityDao() {
		return entityDao;
	}

	public void setEntityDao(EntityDao<T, ID> entityDao) {
		this.entityDao = entityDao;
	}

	public EntityManagerImpl(){
	}

	public T saveEntity(T entity){
		return entityDao.saveEntity(entity);
	}
	
	public void updateEntity(T entity){
		entityDao.updateEntity(entity);
	}
	
	public void deleteEntity(T entity){
		entityDao.deleteEntity(entity);
	}
	
	public void deleteEntityById(ID id){
		entityDao.deleteEntityById(id);
	}
	@Override
	public void deleteEngtityFlagByIds(Class<T> c, String[] ids) {
		if (ids == null ||  ids.length==0)
			return;
		Long[] idsLong = new Long[ids.length];
		for (int i=0;i<ids.length;i++) {
			idsLong[i] = new Long(ids[i]);
		}
		DetachedCriteria criteria = DetachedCriteria.forClass(c);
		criteria.add(Restrictions.in("id", idsLong));
		List catalogs = entityDao.findByCriteria(criteria);
		Iterator it = catalogs.iterator();
		while (it.hasNext()) {
			((BaseEntity)it.next()).setIsDeleted(BaseEntity.FLAG_Y);
		}
		//sessionDao.saveOrUpdateAll(catalogs);
	}
	public T findById(ID id){
		return entityDao.findById(id);
	}
	
	public T findById(Class<T> c, ID id) {
		return entityDao.findById(c, id);
	}

	public T loadById(ID id) {
		return entityDao.loadById(id);
	}
	
	public T loadById(Class<T> c, ID id) {
		return entityDao.loadById(c, id);
	}
 
	public List<T> loadAll() {
		return entityDao.loadAll();
	}
	
	public void saveOrUpdate(T entity) { 
		entityDao.updateEntity(entity); 
	} 
	public List<T> findByHql (String queryString) {
		return entityDao.findByHql(queryString);
	}

	public List<T> findByHql(String queryString, Object[] params) {
		return entityDao.findByHql(queryString, params);
	}
	
	/**
	 * 
	 * 
	 * @param selectString select clause
	 * @param queryString from plus where clause
	 * @param params
	 * @param page
	 * @param orderList order clause
	 * @return PaginatedListImpl,包含分页信息的对象列表。对象为根对象。
	 */
	public List<T> findByHql(String selectString, String queryString, Object[] params, Page page, List<String> orderList) {
		return entityDao.findByHql(selectString, queryString, params, page, orderList);
	}
	
	/**
	 * 分页查询数据----抽象原来的分页查询方式。
	 * @param dc
	 * @param page
	 * @return
	 */
	public List<T> finaByCriteria(final DetachedCriteria dc, final Page page){
		return entityDao.findByCriteria(dc, page);
	}

	public void saveOrUpdateAll(Collection<T> entities) {
		entityDao.saveOrUpdateAll(entities);
	}
	
	/** 
	* 使用指定的检索标准检索数据
	* 
	* @param criteria 
	* @return 
	*/ 
	public List<T> findByCriteria(DetachedCriteria criteria){
		return entityDao.findByCriteria(criteria);
	}
	
	/**
	 * 校验某属性值是否存在
	 */
	@Override
	public boolean checkExistence(DetachedCriteria criteria){
		List<T> list = this.findByCriteria(criteria);
		if (list==null || list.size()==0)
			return false;
		else
			return true;
	}
	/** 
	* 通过合并的方式更新对象
	* 
	* @param entity 
	* void 
	*/ 
	public void merge(T entity){
		entityDao.merge(entity);
	}
	
	/**
	 * 按属性查找唯一对象.
	 */
	public T findUniqueByProperty(String propertyName, Object value){
		return entityDao.findUniqueByProperty(propertyName, value);
	}
	
	/** 
	* 使用带参数的命名HSQL语句查询数据 
	* 
	* @param queryName 
	* @param values 
	* @return 
	*/ 
	public List<T> findByNamedQuery(String queryName, Object[] values){
		if(values != null)
			return entityDao.findByNamedQuery(queryName, values);
		else
			return entityDao.findByNamedQuery(queryName);
	}

	@Override
	public int updateByNamedQuery(String queryName, Object[] values) {
		return entityDao.updateByNamedQuery(queryName, values);
	}
	@Override
	public List<T> findByCriteria(DetachedCriteria criteria, Page page) {
		return entityDao.findByCriteria(criteria, page);
	}
	/**
	 * 根据criteria和orderList进行分页查询。
	 * 
	 * 出于性能考虑排序信息请勿放在criteria里，由本方法负责。
	 * 
	 * @param criteria
	 *            查询条件
	 * @param page
	 *            分页信息
	 * @param orderList
	 *            定制化的排序列表
	 * @return 包含分页信息的对象列表。对象为根对象。
	 */
	public List<T> findByCriteria(DetachedCriteria criteria,Page page, List<Order> orderList){
		return entityDao.findByCriteria(criteria, page, orderList);
	}
	public List<T> createSQLQuery(String nativeSql){
		return entityDao.createSQLQuery(nativeSql);
	}
	@Override
	public List<T> findByCriteriaForJQGrid(DetachedCriteria criteria,
			Page page, List<Order> orderList) {
		return entityDao.findByCriteriaForJQGrid(criteria, page, orderList);
	}
	@Override
	public List<T> findByCriteriaForJQGrid(DetachedCriteria criteria, Page page) {
		return entityDao.findByCriteriaForJQGrid(criteria, page);
	}
}
